import { createStore } from 'vuex'

const setLocalCartList = (state) => {
  const {cartList} = state;
  const cartListString = JSON.stringify(cartList);
  localStorage.cartList = cartListString;
}

const getLocalCartList = ()=>{
  return JSON.parse(localStorage.cartList||'{}') ;

  //或则这么写
  /*try {
    return JSON.parse(localStorage.cartList);
  } catch (e){
    return {};
  }*/
}

export default createStore({
  state: {
    /*
      cartList: {
        // 第一层及是商铺的id
        <shopId>: {
          shopName: '沃尔玛',
          productList: {
            // 商品id
            <itemId>: {
              title: '番茄250g/份',
              imgUrl: 'http://www.dell-lee.com/imgs/vue3/tomato.png',
              sales: 10,
              price: 33.6,
              oldPrice: 39.6,
              count: 2,
              check: false
            },
            <itemId2>: { }
            ...
          }
        },

        <shopId2>: {}
        ...
      }
    */
    cartList: getLocalCartList(),
    addressList: []
  },
  mutations: {
    changeCartItemInfo(state,payload){
      const {shopId, productId, productInfo, num} = payload;
      // console.log('mutations shopId, productId, productInfo:', shopId, productId, productInfo);
      let shopInfo = state.cartList[shopId] || {
        shopName:'',
        productList:{}
      };
      let product = shopInfo.productList[productId];
      if (!product) {
        productInfo.count = 0;
        product = productInfo;
      }
      product.count += num;
      if(payload.num > 0) product.check = true;
      if(product.count<0) product.count = 0;
      shopInfo.productList[productId] = product;
      state.cartList[shopId] = shopInfo;
      setLocalCartList(state);
    },
    changeShopName(state, payload){
      const {shopId, shopName} = payload;
      const shopInfo = state.cartList[shopId] || {
        shopName: '',
        productList: {}
      }
      shopInfo.shopName = shopName;
      state.cartList[shopId] = shopInfo;
      setLocalCartList(state);
    },
    changeCartItemChecked(state,payload){
      const {shopId, productId} = payload;
      const product = state.cartList[shopId].productList[productId];
      product.check = !product.check;
      setLocalCartList(state);
    },
    cleanCartProducts(state,payload){
      const {shopId} = payload;
      state.cartList[shopId].productList = {};
      setLocalCartList(state);
    },
    setCartItemsChecked(state,payload){
      const {shopId} = payload;
      const products = state.cartList[shopId].productList;
      if(products){
        for (let key in products) {
          const product = products[key];
          if(product.count>0) product.check = true;
        }
      }
      setLocalCartList(state);
    },
    clearCartData(state,shopId){
      state.cartList[shopId].productList = {}
      setLocalCartList(state);
    },
    changeAddressList(state, addressList) {
      state.addressList.splice(0, state.addressList.length, ...addressList)
    }
  },
  actions: {
  },
  modules: {
  }
})
